<?php

include_once BONE_ROOT.DS.'controllers'.DS.'vars.php';

class controller_message extends controller_theme
{

	public function __construct()
	{
		parent::__construct();
	}
	
	// 留言列表
	public function listing()
	{
		$template = bone::get_template('message.listing');
		$template->set_title('留言');

		$model_message = bone::get_model('message');

		$limit = 5;
		$pagination = $template->get_pagination();
		$pagination->set_limit( $limit );
		$pagination->set_total($model_message->get_sum());
		$pagination->set_page(get('page', 1, 'int'));
		$pagination->set_url('./?controller=message&task=listing&menu_id='.get('menu_id', 0, 'int'));

		$messages = $model_message->get_list($pagination->get_offset(), $limit);
		$model_user = bone::get_model('user');
		foreach($messages as $message)
		{
			$user = null;
			$manager = null;
			if($message->user_id) $user = $model_user->get($message->user_id);
			if($message->manager_id) $manager = $model_user->get($message->manager_id);

			$message->user = $user;
			$message->manager = $manager;
		}

		$template->set('messages', $messages);
		$template->set_data($this->get_data());
		$template->display();
	}


	public function ajax_save()
	{
		$session = bone::get_session();
		$message_captcha = $session->get('message_captcha');
		$captcha = post('captcha','');
		if(strtolower($message_captcha) != strtolower($captcha))
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '验证码错误');
			$this->ajax();
		}
		
		$name = post('name', null);
		if(!$name)
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', '用户名不能为空');
			$this->ajax();
		}

		$title = post('title', null);
		if(!$title)
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', '留言内容不能为空');
			$this->ajax();
		}

		$email = post('email', null);
		if($email)
		{
			if( !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $email) )
			{
				$this->set_ajax('status', 3);
				$this->set_ajax('description', '邮箱格式非法');
				$this->ajax();
			}
		}

		$table_message = bone::get_table('message');
		$table_message->bind( post() );
		$table_message->name = htmlspecialchars($table_message->name);
		$table_message->title = htmlspecialchars($table_message->title);
		$table_message->ip = $_SERVER['REMOTE_ADDR'];
		$table_message->create_time = time();
		$table_message->save();

		$session->clear('message_captcha');

		$this->set_ajax('status', 0);
		$this->set_ajax('description', '留言保存成功');
		$this->ajax();
	}

	public function ajax_reply()
	{
		$message_id = post('message_id', 0, 'int');
		if(!$message_id) exit;
		
		$my = bone::get_user();
		if($my->guest) exit;
		if(!$my->is_admin && !$my->is_developer) exit;

		$reply = post('reply', '');
		if(!$reply)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '回复内容不能为空');
			$this->ajax();
		}

		$table_message = bone::get_table('message');
		$table_message->load($message_id);
		$table_message->manager_id = $my->id;
		$table_message->reply = htmlspecialchars($reply);
		$table_message->reply_time = time();
		$table_message->save();

		if($table_message->email)
		{
			$config = bone::get_config('system');
			$subject = '您在 '. $config->site_name.' 上的留言已有回复';
			$body = '您于'. date('Y-m-d H:i:s', $table_message->create_time) .' 发表的留言收到了回复.<br><br>';
			$body .= '<strong>留言原文:</strong><br>';
			$body .= $table_message->title;
			$body .= '<br><br><strong>'.$my->name.'于'.date('Y-m-d H:i:s').' 回复:</strong><br>';
			$body .= $table_message->reply;
			$body .= '<br><br>您同时也可以访问 <a href="'.$config->site_url.'" target="_blank">'.$config->site_name.'</a>进行查看';
			
			$mail = bone::get('mail');
			$mail->set_subject($subject);
			$mail->set_body($body);
			$mail->to($table_message->email);
			$mail->send();
		}

		$this->set_ajax('status', 0);
		$this->set_ajax('description', '回复成功');
		$this->ajax();
	}

	public function ajax_delete()
	{
		$message_id = get('message_id', 0, 'int');
		if(!$message_id) exit;
		
		$my = bone::get_user();
		if($my->guest) exit;

		$table_message = bone::get_table('message');
		$table_message->load($message_id);

		if($my->id==$table_message->user_id || $my->is_admin)
		{
			$table_message->delete();
			$this->set_ajax('status', 0);
			$this->set_ajax('description', '删除成功');
			$this->ajax();
		}
		else
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '您没有权限');
			$this->ajax();
		}
	}

	public function ajax_delete_reply()
	{
		$message_id = get('message_id', 0, 'int');
		if(!$message_id) exit;
		
		$my = bone::get_user();
		if($my->guest) exit;

		$table_message = bone::get_table('message');
		$table_message->load($message_id);

		if($my->id==$table_message->manager_id || $my->is_admin)
		{
			$table_message->manager_id = 0;
			$table_message->reply = '';
			$table_message->reply_time = 0;
			$table_message->save();

			$this->set_ajax('status', 0);
			$this->set_ajax('description', '删除成功');
			$this->ajax();
		}
		else
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '您没有权限');
			$this->ajax();
		}
	}


}
?>